#include<stdio.h>
#include<stdlib.h>

int search(int A[],int length,int key)
{
    int low=0,high=length-1,mid;
    while(low<=high)
    {
        mid=(low+high)/2;

        if(A[mid]==key)
        {
            return A[mid];
        }
        else if(A[mid]>key)
        {
            high=mid-1;
        }
        else
        {
            low=mid+1;
        }
    }
    return 0;
}

int cmp(const void *a,const void *b)
{
    return (*(int*)a)>(*(int*)b)?1:-1;
}

int main()
{
    int a[10]={2, 3, 9, 54, 62, 58, 32, 65, 87, 5};
    qsort(a,10,sizeof(a[0]),cmp);
    int i;
    for(i=0;i<10;i++)
    {
        printf("%d ",a[i]);
    }
    printf("=%d\n",search(a,10,2));
    printf("=%d\n",search(a,10,100));
    printf("=%d\n",search(a,10,23));
    printf("=%d\n",search(a,10,62));
    printf("=%d\n",search(a,10,87));
}
